﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_User")]
    public class T_User : IEntity<T_User>
    {
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        [Key]
        public string UserName { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Password { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string NickName { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string RealName { get; set; }

        [Column(TypeName = "bit")]
        public bool Gender { get; set; }

        [Column(TypeName = "datetime")]
        public DateTime Birthday { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string QQ { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string Phone { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string PhoneOne { get; set; }

        [Column(TypeName="varchar")]
        [StringLength(256)]
        public string Photo { get; set; }

        [Column(TypeName = "datetime")]
        public DateTime CreateDate { get; set; }

        [Column(TypeName = "bit")]
        public bool IsRefuse { get; set; }

        [Column(TypeName = "bit")]
        public bool IsActivate { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string Token { get; set; }

        public int SystemRole { get; set; }

        public int CollegeId { get; set; }

        public int FloorId { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Dormitory { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string IdCardNo { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string RebateLevel { get; set; }

        /// <summary>
        /// 院系
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Departments { get; set; }
        /// <summary>
        /// 班级
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Classes { get; set; }

        public override T_User Find(System.Linq.Expressions.Expression<Func<T_User, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.User.FirstOrDefault(where);
                if (item != null)
                {
                    this.UserName = item.UserName;
                    this.Password = item.Password;
                    this.NickName = item.NickName;
                    this.RealName = item.RealName;
                    this.Gender = item.Gender;
                    this.Birthday = item.Birthday;
                    this.QQ = item.QQ;
                    this.Phone = item.Phone;
                    this.Photo = item.Photo;
                    this.CreateDate = item.CreateDate;
                    this.IsRefuse = item.IsRefuse;
                    this.IsActivate = item.IsActivate;
                    this.Token = item.Token;
                    this.SystemRole = item.SystemRole;
                    this.CollegeId = item.CollegeId;
                    this.FloorId = item.FloorId;
                    this.Dormitory = item.Dormitory;
                    this.IdCardNo = item.IdCardNo;
                    this.RebateLevel = item.RebateLevel;
                    this.PhoneOne = item.PhoneOne;
                    this.Departments = item.Departments;
                    this.Classes = item.Classes;
                    return this;
                }
                return null;
            }
        }
        public override T_User Find(string key)
        {
            return Find(c=>c.UserName == key);
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_User, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.User.Any(where);
            }
        }

        public override T_User FindLast(System.Linq.Expressions.Expression<Func<T_User, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.User.OrderByDescending(c=>c.CreateDate).FirstOrDefault(where);
                if (item != null)
                {
                    this.UserName = item.UserName;
                    this.Password = item.Password;
                    this.NickName = item.NickName;
                    this.RealName = item.RealName;
                    this.Gender = item.Gender;
                    this.Birthday = item.Birthday;
                    this.QQ = item.QQ;
                    this.Phone = item.Phone;
                    this.Photo = item.Photo;
                    this.CreateDate = item.CreateDate;
                    this.IsRefuse = item.IsRefuse;
                    this.IsActivate = item.IsActivate;
                    this.Token = item.Token;
                    this.SystemRole = item.SystemRole;
                    this.CollegeId = item.CollegeId;
                    this.FloorId = item.FloorId;
                    this.Dormitory = item.Dormitory;
                    this.IdCardNo = item.IdCardNo;
                    this.RebateLevel = item.RebateLevel;
                    this.PhoneOne = item.PhoneOne;
                    this.Departments = item.Departments;
                    this.Classes = item.Classes;
                    return this;
                }
                return null;
            }
        }
    }
}
